package cn.dyp.com.logprint.ui;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import cn.dyp.com.logprint.R;
import cn.dyp.com.logprint.bean.LogBean;


/**
 * 自定义适配器类
 * 
 * @author asus
 *
 */
public class LogListAdapter extends ArrayAdapter<LogBean> {
	private LayoutInflater mInflater;
	private ArrayList<LogBean> mDataLists;

	/**
	 * 构造函数
	 * 
	 * @param context
	 *            上下文对象
	 * @param dataLists
	 *            数据源
	 */
	public LogListAdapter(Context context, ArrayList<LogBean> dataLists) {
		super(context, 0, dataLists);
		mDataLists = dataLists;
		mInflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	/**
	 * 获取数据源长度
	 * 
	 * @see ArrayAdapter#getCount()
	 */
	@Override
	public int getCount() {
		return mDataLists.size();
	}

	/**
	 * 获取当前数据
	 * 
	 * @see ArrayAdapter#getItem(int)
	 */
	@Override
	public LogBean getItem(int pos) {
		return mDataLists.get(pos);
	}

	/**
	 * 获取Item位置
	 * 
	 * @see ArrayAdapter#getItemId(int)
	 */
	@Override
	public long getItemId(int pos) {
		return pos;
	}

	/**
	 * 获取自定义视图
	 * 
	 * @see ArrayAdapter#getView(int, View,
	 *      ViewGroup)
	 */
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.list_item, parent,
					false);
		}
		TextView dataTimeTV = (TextView) convertView
				.findViewById(R.id.data_time_text_view);
		TextView ipTV = (TextView) convertView.findViewById(R.id.ip_text_view);
		TextView userNameTV = (TextView) convertView
				.findViewById(R.id.username_text_view);
		TextView opTypeTV = (TextView) convertView
				.findViewById(R.id.operation_type_text_view);
		TextView opObjTV = (TextView) convertView
				.findViewById(R.id.operation_obj_text_view);
		TextView logTV = (TextView) convertView
				.findViewById(R.id.log_text_view);
		LogBean logBean = getItem(position);
		if (logBean != null) {
			dataTimeTV.setText(logBean.getDate());
			ipTV.setText(logBean.getClientIP());
			userNameTV.setText(logBean.getUsername());
			opTypeTV.setText(logBean.getOperationType());
			opObjTV.setText(logBean.getOperationObj());
			logTV.setText(logBean.getLogMsg());
		}
		return convertView;
	}
}
